package com.yuncheng.spcyApi.spcy;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.busService.InitWjjlService;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.flow.FlowVo;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.flow.SpcySjxdFlowService;
import com.yuncheng.spcyApi.mapper.SpcySjxdJbxxMapper;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.IpRateLimiterUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.flow.SpcySjxdFlowVo;
import com.yuncheng.spcyApi.vo.flow.SpcySjxdFqVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyReturnVo;
import com.yuncheng.spcyApi.vo.spcy.SpcySjxdXpryVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author hua
 */
@Api(tags = "数据修订流程接口")
@RestController
@RequestMapping("/api/spzx-spcy/sjxd")
public class SpcySjxdFlowController {

    @Resource
    private SpcySjxdJbxxMapper spcySjxdJbxxMapper;

    @Resource
    private SpcySjxdFlowService spcySjxdFlowService;

    @Resource
    private InitWjjlService initWjjlService;

    @Resource
    private BusService busService;

    @Log(title = "数据修订-待办列表-分页查询")
    @ApiOperation(value = "数据修订-待办列表-分页查询")
    @GetMapping(value = "sjxdDbFlowJob")
    public HttpResult sjxdDbFlowJob(SpcySjxdFlowVo flowVo, PageVo pageVo, OrderItemVo orderItemVo){

        flowVo.setFblzt(SpcyConstant.BLZT_DB);
        Page<SpcySjxdFlowVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        IPage<SpcySjxdFlowVo> list = spcySjxdJbxxMapper.flowDbJobPage(page,GetCurrentUserUtils.getCurrentUserId(), flowVo, orderItemVo);
        return HttpResult.ok(list);
    }

    @Log(title = "数据修订-已办列表-分页查询")
    @ApiOperation(value = "数据修订-已办列表-分页查询")
    @GetMapping(value = "sjxdYbFlowJob")
    public HttpResult sjxdYbFlowJob(SpcySjxdFlowVo flowVo, PageVo pageVo, OrderItemVo orderItemVo){

        flowVo.setFblzt(SpcyConstant.BLZT_YB);
        Page<SpcySjxdFlowVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        IPage<SpcySjxdFlowVo> list = spcySjxdJbxxMapper.flowYbJobPage(page,GetCurrentUserUtils.getCurrentUserId(), flowVo, orderItemVo);
        return HttpResult.ok(list);
    }

    @Log(title = "发起审评数据修订流程-发起申请")
    @ApiOperation(value = "发起审评数据修订流程-发起申请")
    @PostMapping(value = "fqsjxdFlow")
    public HttpResult fqsjxdFlow(@RequestBody SpcySjxdFqVo paramVo){

        return spcySjxdFlowService.initSjxdData(paramVo);
    }

    @Log(title = "数据修订-删除数据修订信息-根据流程任务id(撤销申请)")
    @ApiOperation(value = "数据修订-删除数据修订信息-根据流程任务id(撤销申请)")
    @DeleteMapping(value = "deleteSjxdInfo")
    public HttpResult deleteSjxdInfo(@RequestParam(value = "jobid",defaultValue = "") @ApiParam("流程id") String jobid){

        return spcySjxdFlowService.deleteSjxdData(jobid);
    }

    @Log(title = "数据修订-查询流程历史记录")
    @ApiOperation(value = "数据修订-查询流程历史记录")
    @GetMapping(value = "queryJobHistoryList")
    public HttpResult queryJobHistoryList(@RequestParam(value = "id",defaultValue = "") @ApiParam("数据修订id") String fsjxdid){

        return spcySjxdFlowService.queryJobHistoryList(fsjxdid);
    }

    @Log(title = "数据修订-流程保存接口")
    @ApiOperation(value = "数据修订-流程保存接口")
    @PostMapping(value = "/saveFlow")
    public HttpResult<?> saveFlow(@RequestBody FlowVo flowVo) {
        String submitType = SpcyFlowConstant.SUBMIT_TYPE_SAVE; // 保存

        if (StringUtils.isBlank(flowVo.getJobid())){
            return HttpResult.error("流程任务参数丢失，请联系管理员！");
        }

        // 流程处理
        Result httpResult = spcySjxdFlowService.handleFlow(flowVo,  flowVo.getJobid(), "", submitType, flowVo.getOpinion());
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "数据修订-流程提交接口")
    @ApiOperation(value = "数据修订-流程提交接口")
    @PostMapping(value = "/submitFlow")
    public HttpResult<?> submitFlow(@RequestBody FlowVo flowVo) {
        // 接口限流
        if (!IpRateLimiterUtils.tryAcquire(GetCurrentUserUtils.getCurrentUserId())){
            return HttpResult.error("访问过于频繁，请稍候再试！");
        }

        String submitType = SpcyFlowConstant.SUBMIT_TYPE_SUBMIT; // 提交
        if (StringUtils.isBlank(flowVo.getJobid())){
            return HttpResult.error("流程任务参数丢失，请联系管理员！");
        }

        Result httpResult1 = spcySjxdFlowService.handleFlow(flowVo, flowVo.getJobid(), "", submitType, flowVo.getOpinion());
        if (!httpResult1.isSuccess()){
            return HttpResult.error(httpResult1.getMessage());
        }

        return HttpResult.ok("提交成功");
    }

    @Log(title = "数据修订-退回上一环节流程任务")
    @ApiOperation(value = "数据修订-退回上一环节流程任务")
    @PostMapping(value = "returnFlowJob")
    public HttpResult<?> returnFlowJob(@RequestBody SpcyReturnVo spcyReturnVo){
        // 接口限流
        if (!IpRateLimiterUtils.tryAcquire(GetCurrentUserUtils.getCurrentUserId())){
            return HttpResult.error("访问过于频繁，请稍候再试！");
        }

        return spcySjxdFlowService.getReturnUpperFlowJob(spcyReturnVo);
    }

    @Log(title = "数据修订-查询流程任务详情")
    @ApiOperation(value = "数据修订-查询流程任务详情")
    @GetMapping(value = "getJobConcernByJobId")
    public HttpResult getJobConcernByJobId(@RequestParam(value = "jobid",defaultValue = "") @ApiParam("流程id") String jobid){

        return HttpResult.ok(spcySjxdFlowService.getJobConcernByJobId(jobid, null, null));
    }

    @Log(title = "数据修订-查询数据修订方案文件列表")
    @ApiOperation(value = "数据修订-查询数据修订方案文件列表")
    @GetMapping(value = "querySjxdFaWjList")
    public HttpResult querySjxdFaWjList(@RequestParam(value = "id",defaultValue = "") @ApiParam("数据修订id") String fsjxdid){

        return spcySjxdFlowService.selectSjxdFaWjList(fsjxdid);
    }

    @Log(title = "数据修订-上传修订数据-方案文件")
    @ApiOperation(value = "数据修订-上传修订数据-方案文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "fsjxdid",value = "数据修订id")
    })
    @PostMapping(value = "uploadSjxdFile")
    public HttpResult uploadSjxdFile(@RequestPart("file") MultipartFile[] uploadFile,
                                     @RequestParam(value = "id",defaultValue = "") @ApiParam("数据修订id") String fsjxdid){
        if (uploadFile == null || uploadFile.length == 0) {
            return HttpResult.error("上传失败,请先选择文件！");
        }

        if (StringUtils.isBlank(fsjxdid)){
            return HttpResult.error("修订参数为空！");
        }

        initWjjlService.deleteCommontSqsxFile(fsjxdid, WjmlConstant.jcfa);
        initWjjlService.uploadSqsxFile(uploadFile, fsjxdid, WjmlConstant.jcfa, "",false);

        return HttpResult.ok("上传成功");
    }

    @Log(title = "数据修订-查询修订基本信息-根据修订数据id")
    @ApiOperation(value = "数据修订-查询修订基本信息-根据修订数据id")
    @GetMapping(value = "queryXdJbxxById")
    public HttpResult queryXdJbxxById(@RequestParam(value = "id",defaultValue = "") @ApiParam("数据修订id") String fsjxdid){
        if (StringUtils.isBlank(fsjxdid)){
            return HttpResult.error("修订参数为空！");
        }

        return HttpResult.ok(spcySjxdJbxxMapper.selectById(fsjxdid));
    }

    @Log(title = "数据修订-更新数据修订信息")
    @ApiOperation(value = "数据修订-更新数据修订信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "修订数据id"),
            @ApiImplicitParam(name = "fsqrq",value = "申请日期"),
            @ApiImplicitParam(name = "fsqly",value = "申请理由"),
            @ApiImplicitParam(name = "fxdjckssj",value = "修订检查开始时间"),
            @ApiImplicitParam(name = "fxdjcjssj",value = "修订检查结束时间")
    })
    @PostMapping(value = "updateSjxdInfo")
    public HttpResult updateSjxdInfo(@RequestBody HashMap<String,Object> buffer){

        Result result = spcySjxdFlowService.updateSjxdData(buffer, "");
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        return HttpResult.ok("保存成功");
    }

    @Log(title = "数据修订-查询新增修订人员-列表")
    @ApiOperation(value = "数据修订-查询新增修订人员-列表")
    @ApiImplicitParam(name = "id", value = "数据修订id", required = true)
    @GetMapping(value = "queryAddSjxdUserList")
    public HttpResult queryAddSjxdUserList(PageVo pageVo, RsHcyVo query){

        return spcySjxdFlowService.selectAddSjxdUserList(query, query.getId(), pageVo);
    }

    @Log(title = "数据修订-查询移除修订人员-列表")
    @ApiOperation(value = "数据修订-查询移除修订人员-列表")
    @GetMapping(value = "queryYcXpryList")
    public HttpResult queryYcXpryList(@RequestParam(value = "id") @ApiParam("数据修订id") String fsjxdid){

        return spcySjxdFlowService.selectRemoveSjxdUserList(fsjxdid);
    }

    @Log(title = "查询数据修订人员列表")
    @ApiOperation(value = "查询数据修订人员列表")
    @GetMapping(value = "querySjxdRyList")
    public HttpResult querySjxdRyList(@RequestParam(value = "id") @ApiParam("数据修订id") String fsjxdid){

        return spcySjxdFlowService.selectSjxdRyList(fsjxdid);
    }


    @Log(title = "数据修订-新增修订人员")
    @ApiOperation(value = "数据修订-新增修订人员")
    @PostMapping(value = "addSjxdXpry")
    public HttpResult addSjxdXpry(@RequestBody SpcySjxdXpryVo paramVo){

        return spcySjxdFlowService.addSjxdXpryData(paramVo);
    }

    @Log(title = "数据修订-删除修订人员")
    @ApiOperation(value = "数据修订-删除修订人员")
    @DeleteMapping(value = "deleteSjxdXpry")
    public HttpResult deleteSjxdXpry(@RequestParam(value = "id") @ApiParam("修订人员id") String id){

        return spcySjxdFlowService.deleteSjxdXpryData(id);
    }

    @Log(title = "数据修订-初始化文件模板")
    @ApiOperation(value = "数据修订-初始化文件模板")
    @GetMapping(value = "initSjxdMb")
    public HttpResult initSjxdMb(@RequestParam(value = "id",defaultValue = "") @ApiParam("数据修订id") String fsjxdid,
                                 @RequestParam(value = "fmbid",defaultValue = "") @ApiParam("选择的文件模板id") String wjmbId) {
        if (StringUtils.isBlank(fsjxdid)) {
            return HttpResult.error("操作失败，修订参数为空！");
        }

        String sqsxid = spcySjxdJbxxMapper.selectXdSqsxIdById(fsjxdid);
        Result result = busService.initSyncOtherCommonWjmb(fsjxdid, sqsxid, WjmlConstant.jcfa, wjmbId, true); // 初始化文件记录
        if (!result.isSuccess()) {
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("操作成功");
    }

}
